﻿Imports System.Globalization

Public Class frmListaPrecio

    Dim controllerStock As New ControllerSigeco.controller_stock

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Me.Close()
    End Sub

    Private _codListaPrecio As Integer = 0

    Public Property codListaPrecio() As Integer
        Get
            Return _codListaPrecio
        End Get
        Set(ByVal value As Integer)
            _codListaPrecio = value
        End Set
    End Property

    Private _codMercaderia As Integer = 0

    Public Property codMercaderia() As Integer
        Get
            Return _codMercaderia
        End Get
        Set(ByVal value As Integer)
            _codMercaderia = value
        End Set
    End Property

    Private _codMercaderiaDetalle As Integer = 0

    Public Property codMercaderiaDetalle() As Integer
        Get
            Return _codMercaderiaDetalle
        End Get
        Set(ByVal value As Integer)
            _codMercaderiaDetalle = value
        End Set
    End Property

    Private _soloLectura As Boolean = False
    Public Property soloLectura() As Boolean
        Get
            Return _soloLectura
        End Get
        Set(ByVal value As Boolean)
            _soloLectura = value
        End Set
    End Property

    Private m_frmBusMercaderia As frmMercaderiaBuscador
    Public Property frmBusMercaderia() As frmMercaderiaBuscador
        Get
            Return m_frmBusMercaderia
        End Get
        Set(ByVal value As frmMercaderiaBuscador)
            m_frmBusMercaderia = value
        End Set
    End Property

    Private Sub btnBuscMercaderia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscMercaderia.Click

        frmBusMercaderia = New frmMercaderiaBuscador
        frmBusMercaderia.ShowDialog()
        txtMercaderia.Text = frmBusMercaderia.MercaderiaDesc
        codMercaderia = frmBusMercaderia.codMercaderia
        If codMercaderia > 0 Then
            txtprecioa.Focus()
        End If

    End Sub

    Private Sub frmCompras_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dgvdetalle.AutoGenerateColumns = False

        CargarListaPrecios()

    End Sub

    Private Sub CargarListaPrecioDetalle()
        Try

            codMercaderia = 0
            codMercaderiaDetalle = 0
            txtMercaderia.Text = ""
            txtprecioa.Text = 0
            txtpreciob.Text = 0
            txtprecioc.Text = 0

            Dim ds As New DataTable
            ds = controllerStock.get_listas_precios_detalles(oiml.dbConn, codListaPrecio, "", 0, "", "", 0, 0)

            If Not ds Is Nothing Then
                dgvdetalle.DataSource = ds

                If txtAprobadoPor.Text = "" Then
                    btnConfirmar.Enabled = True
                Else
                    btnConfirmar.Enabled = False
                End If

            Else
                dgvdetalle.DataSource = Nothing
            End If

            txtMercaderia.Focus()

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Dim ci As CultureInfo

    Private Sub CargarListaPrecios()
        Try
            If codListaPrecio > 0 Then

                Dim ds As New DataTable
                ds = controllerStock.get_listas_precios_by_id(oiml.dbConn, codListaPrecio)

                If Not ds Is Nothing Then

                    txtDescripcion.Text = ds.Rows(0)("descripcion")
                    fechaInicio.Value = ds.Rows(0)("fechaInicio")
                    fechaFin.Value = ds.Rows(0)("fechaFin")
                    txtAprobadoPor.Text = ds.Rows(0)("aprobadoPor").ToString()

                    ControlEstados()

                    txtActualizadoFecha.Text = ds.Rows(0)("actualizadoPor_")
                    txtActualizadoPor.Text = ds.Rows(0)("usuario")

                    CargarListaPrecioDetalle()

                End If

            Else

                txtDescripcion.Focus()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub ControlEstados()

        If txtAprobadoPor.Text <> "" Then

            txtDescripcion.Enabled = False
            fechaInicio.Enabled = False
            fechaFin.Enabled = False

            btnBuscMercaderia.Enabled = False

            btnGrabar.Enabled = False
            btnAgregarDetalle.Enabled = False

            btnConfirmar.Visible = False

            btnEliminar.Enabled = False
            btnEliminarDetalle.Enabled = False

            txtprecioa.Enabled = False
            txtpreciob.Enabled = False
            txtprecioc.Enabled = False

            btnCancelar.Focus()

        End If

    End Sub

    Private Function ValidarCampos() As Boolean

        Dim controlCampos As Boolean = False

        If txtDescripcion.Text = "" Then
            ErrorProvider1.SetError(txtDescripcion, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtDescripcion, "")
        End If

        If fechaInicio.Value.ToString = "" Then
            ErrorProvider1.SetError(fechaInicio, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(fechaInicio, "")
        End If

        If fechaFin.Value.ToString = "" Then
            ErrorProvider1.SetError(fechaFin, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(fechaFin, "")
        End If

        Return controlCampos

    End Function

    Private Function ValidarCamposDetalle() As Boolean

        Dim controlCampos As Boolean = False

        If txtMercaderia.Text = "" Or codMercaderia = 0 Then
            ErrorProvider1.SetError(txtMercaderia, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtMercaderia, "")
        End If        '

        If txtprecioa.Text = "" Or Not IsNumeric(txtprecioa.Text) Then
            ErrorProvider1.SetError(txtprecioa, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtprecioa, "")
        End If        '

        If txtpreciob.Text = "" Or Not IsNumeric(txtpreciob.Text) Then
            ErrorProvider1.SetError(txtpreciob, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtpreciob, "")
        End If        '

        If txtprecioc.Text = "" Or Not IsNumeric(txtprecioc.Text) Then
            ErrorProvider1.SetError(txtprecioc, "Campo obligatorio")
            controlCampos = True
        Else
            ErrorProvider1.SetError(txtprecioc, "")
        End If        '

        Return controlCampos

    End Function

    Private Sub btnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminar.Click
        If codListaPrecio > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Lista de precio", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controllerStock.Delete_lista_precio(oiml.dbConn, codListaPrecio) Then
                    Me.Close()

                Else
                    MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precio", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If codListaPrecio = 0 Then
            codListaPrecio = controllerStock.Insert_lista_precio(oiml.dbConn, fechaInicio.Value, fechaFin.Value, 0, txtDescripcion.Text, oiml.idUsuario)

            If codListaPrecio > 0 Then
                CargarListaPrecios()
                MessageBox.Show("Registro grabado con exito!", "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        Else
            If controllerStock.Update_lista_precio(oiml.dbConn, codListaPrecio, fechaInicio.Value, fechaFin.Value, 0, txtDescripcion.Text, oiml.idUsuario) Then
                CargarListaPrecios()
                MessageBox.Show("Registro grabado con exito!", "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)


            Else
                MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End If

    End Sub

    Private Sub dgvdetalle_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvdetalle.KeyDown
        If e.KeyCode = Keys.Enter Then
            If dgvdetalle.SelectedRows.Count > 0 Then

                codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If
        End If
    End Sub

    Private Sub dgvdetalle_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvdetalle.DoubleClick
        If dgvdetalle.SelectedRows.Count > 0 Then

            codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

        End If
    End Sub

    Private Sub dgvdetalle_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvdetalle.CellClick
        Try

            If dgvdetalle.SelectedRows.Count > 0 Then

                codMercaderiaDetalle = dgvdetalle.SelectedRows(0).Cells(0).Value

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnEliminarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEliminarDetalle.Click

        If codMercaderiaDetalle > 0 Then

            If MessageBox.Show("Desea eliminar el registro seleccionado?", "Lista de precios", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

                If controllerStock.Delete_lista_precio_detalle(oiml.dbConn, codMercaderiaDetalle, 0) Then
                    CargarListaPrecioDetalle()

                Else
                    MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Error)

                End If

            End If

        End If

    End Sub

    Private Sub btnAgregarDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAgregarDetalle.Click

        If ValidarCampos() Then
            Exit Sub
        End If

        If ValidarCamposDetalle() Then
            Exit Sub
        End If

        If codListaPrecio = 0 Then
            codListaPrecio = controllerStock.Insert_lista_precio(oiml.dbConn, fechaInicio.Value, fechaFin.Value, 0, txtDescripcion.Text, oiml.idUsuario)

            If codListaPrecio > 0 Then
                MessageBox.Show("Registro grabado con exito!", "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Else
                MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        Else
            If controllerStock.Update_lista_precio(oiml.dbConn, codListaPrecio, fechaInicio.Value, fechaFin.Value, 0, txtDescripcion.Text, oiml.idUsuario) Then
                MessageBox.Show("Registro grabado con exito!", "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

        If codListaPrecio > 0 And codMercaderia > 0 Then

            If controllerStock.Insert_lista_precio_detalle(oiml.dbConn, codListaPrecio, codMercaderia, Decimal.Parse(txtprecioa.Text), Decimal.Parse(txtpreciob.Text), Decimal.Parse(txtprecioc.Text)) Then
                CargarListaPrecios()
                MessageBox.Show("Registro grabado con exito!", "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precios", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub btnConfirmar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfirmar.Click

        If codListaPrecio > 0 Then

            If controllerStock.Update_Lista_Precio_Estado(oiml.dbConn, codListaPrecio, oiml.idUsuario) Then
                CargarListaPrecios()
                MessageBox.Show("Registro confirmado con exito!", "Lista de precio", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                MessageBox.Show("Error: " & controllerStock.MessageError.Message, "Lista de precio", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If

    End Sub

    Private Sub txtMercaderia_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMercaderia.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarMercaderias()
        End If
    End Sub

    Private Sub CargarMercaderias()
        Try
            Dim idMarca As Integer = 0
            Dim idRubro As Integer = 0
            Dim ds As New DataTable

            ds = controllerStock.get_mercaderias_All(oiml.dbConn, 0, "", idMarca, idRubro, False, txtMercaderia.Text, 0, 0)

            If Not ds Is Nothing Then

                txtMercaderia.Text = ""
                txtMercaderia.Text = ds.Rows(0)("mercaderia")
                codMercaderia = ds.Rows(0)("idMercaderia")
                If codMercaderia > 0 Then
                    txtprecioa.Focus()
                End If

            Else

                txtMercaderia.Text = ""
                codMercaderia = 0
                MessageBox.Show("Codigo no registrado, favor verifique", "Mercaderias", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub txtMercaderia_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMercaderia.Enter
        txtMercaderia.Text = ""
        codMercaderia = 0
    End Sub

End Class